Skip to content

Extend options arg to support custom model definitions - #96

Closed
shlomiassaf wants to merge 2 commits into
strongloop:masterfrom
shlomiassaf:master
Closed

shlomiassaf wants to merge 2 commits into
strongloop:masterfrom
shlomiassaf:master

Conversation

@shlomiassaf

Copy link
Copy Markdown
Contributor
  • Added the ability to create ad-hoc modelDefinitions.
  • Added the ability to "extend" models-config (choose overwrite/extend)

See #49

* Added the ability to "extend" models-config (choose overwrite/extend)
@slnode

slnode commented Feb 18, 2015

Copy link
Copy Markdown

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

@shlomiassaf

Copy link
Copy Markdown
Contributor Author

Example:

boot(app, {
  appRootDir: __dirname,
  models: {
    "__extend": true,
    "testadhoc": {
      "dataSource": "db",
      "public": true
    },
    "testAdHocManual": {
      "dataSource": "db",
      "public": true
    }
  },
  modelDefinitions: {
    sourceDir: "../common/models",
    definitions: [
      {
        definition: {
          "name": "testadhoc",
          "plural": "testAdhocs",
          "base": "PersistedModel",
          "idInjection": true,
          "properties": {
            "test3": {
              "type": "string"
            },
            "test4": {
              "type": "number"
            }
          }
        }
      },
      {
        definition: {
          "name": "testAdHocManual",
          "plural": "testAdHocManuals",
          "base": "PersistedModel",
          "idInjection": true,
          "properties": {
            "test5": {
              "type": "string"
            },
            "test6": {
              "type": "number"
            }
          }
        },
        sourceFile: "../testAdHocManual123.js" // file is ../common/testAdHocManual123.js
      }
    ]
  }
});

modelDefinitions.sourceDir is relative to appRootDir
modelDefinitions.definition[x].sourceFile is relative to modelDefinitions.sourceDir || appRootDir

If no sourceFile supplied, a search is made in modelDefinitions.sourceDir, the filename is the the lowercase value of definition.name.

TODO: set the Basename to a dasherize name instead of lowercase.

when setting models["__extend"] to true, models are a distinct union of models-config.js and the supplied model config collection object.

models["__extend"] is option, defaults to false

When a models object is added to the options with and explicit "__extend" = false it would skip deletion of the property.

moving the delete instruction will fix it.
@bajtos

bajtos commented Feb 23, 2015

Copy link
Copy Markdown
Member

@shlomiassaf thank you for the pull request. It is very unclear to me what problem (use-case) are you trying to solve? My initial impression is that you are making things too complex and thus I am inclining to reject this patch.

@shlomiassaf

Copy link
Copy Markdown
Contributor Author

@bajtos,

This is an issue/enhancement you opened, as stated in my 1st comment: SEE #49
You even posted a demo boot code...

The use case is very clear, allow loading models using the model definition schema but not from a file, from an object.
It enables loading models from database, remote calls, etc...

@bajtos

bajtos commented Feb 24, 2015

Copy link
Copy Markdown
Member

I see, thanks for the explanation. I'll review the patch with that use case in mind.

@bajtos

bajtos commented Feb 24, 2015

Copy link
Copy Markdown
Member

@slnode test please

@bajtos

bajtos commented Feb 24, 2015

Copy link
Copy Markdown
Member

This patch seems to either implement a different thing, or else it is adding much more features beyond simple "custom model definitions".

Let's split the work into multiple small incremental pull requests, so that I can review each new feature in isolation.

Please start with the feature "allow the developer to supply custom model definitions to replace the result of findModelDefinitions" and add a unit-test to demonstrate & verify the new functionality.

@shlomiassaf

Copy link
Copy Markdown
Contributor Author

I understand what you mean.

A quick question,
Does replacing the result of findModelDefinitions means giving up on internal model definitions (User, ACL, etc..)?

The code as is now, does not allow loading models along with built in models since the models-config is either from file or in options object.

If a user wants to generate models from database, remote call or any other external source, he will be able to do it but he will have to give up the built-in models.

The solution in my PR enables loading them without skipping the models-config.json load.

@bajtos

bajtos commented Feb 25, 2015

Copy link
Copy Markdown
Member

A quick question,
Does replacing the result of findModelDefinitions means giving up on internal model definitions (User, ACL, etc..)?

Yes.

The code as is now, does not allow loading models along with built in models since the models-config is either from file or in options object.

If a user wants to generate models from database, remote call or any other external source, he will be able to do it but he will have to give up the built-in models.

The built-in models are always defined created by loopback, you should not include them in model definitions passed to loopback-boot. Just reference them in model-config.json.

Even if that was not the case, the definition of built-in models can be easily reconstructed:

{
  User: {
    definition: require('loopback/common/models/user.json'),
    sourceFile: require.resolve('loopback/common/models/user.js')
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants